package fule.com.webviewdemo.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import fule.com.webviewdemo.R;
import fule.com.webviewdemo.util.GlideUtils;

/**
 * 作者： njb
 * 时间： 2018/8/31 0031-下午 5:08
 * 描述：
 * 来源：
 */
public class FM_BadyDetail extends Fragment {
    @BindView(R.id.rv_detail)
    RecyclerView rvDetail;
    Unbinder unbinder;
    private View rootView;
    private BaseQuickAdapter<String, BaseViewHolder> adapter;
    private String content;
    private String imgUrl;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.activity_recyclerview_html, container, false);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }


    private void initView() {
        adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_detail, null) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                GlideUtils.loadImgByWrap(getActivity(), item, (ImageView) helper.getView(R.id.iv_detail));
            }
        };
        rvDetail.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvDetail.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        imgUrl = "<img src=\"/images/image/20161104/20161104144203_82117.jpg\" alt=\"\" /><img src=\"/images/image/20161116/20161116165842_88829.jpg\" alt=\"\" /><img src=\"/images/image/20161104/20161104144206_17003.jpg\" alt=\"\" /><img src=\"/images/image/20161104/20161104144207_62404.jpg\" alt=\"\" /><img src=\"/images/image/20161104/20161104144208_78881.jpg\" alt=\"\" />";
        setHtml(imgUrl);
    }

    /**
     * 设置html
     * @param imgUrl
     */
    private void setHtml(final String imgUrl) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                content = imgUrl.replaceAll("src=\"", "src=\"https://www.38.hn");
                String[] ht = content.split("\"");
                List<String> list = new ArrayList<>();

                for (String rx : ht) {
                    if (rx.contains("https")) {
                        list.add(rx);
                    }
                }
                adapter.setNewData(list);
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
